﻿//------------------------------------------------------------------------------
// System Name:   Customer Service Platform
// Component:     All Aera
// Created By:    Miles Chen (milesch@wicresoft.com)
// Created Date:  2011-2-20
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Wicresoft.Web.Controls
{
    public class ActionField : CommandField
    {
        public virtual bool ShowViewButton
        {
            get
            {
                object obj2 = base.ViewState["ShowViewButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowViewButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowViewButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowModifyButton
        {
            get
            {
                object obj2 = base.ViewState["ShowModifyButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowModifyButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowModifyButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowDisableButton
        {
            get
            {
                object obj2 = base.ViewState["ShowDisableButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowDisableButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowDisableButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowInsertMoreButton
        {
            get
            {
                object obj2 = base.ViewState["ShowInsertMoreButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowInsertMoreButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowInsertMoreButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }
        public virtual bool ShowCloseButton
        {
            get
            {
                object obj2 = base.ViewState["ShowCloseButton"];
                return ((obj2 != null) && ((bool)obj2));
            }
            set
            {
                object obj2 = base.ViewState["ShowCloseButton"];
                if ((obj2 == null) || (((bool)obj2) != value))
                {
                    base.ViewState["ShowCloseButton"] = value;
                    this.OnFieldChanged();
                }
            }
        }

        public override bool ShowCancelButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowCancelButton;
            }
            set
            {
                base.ShowCancelButton = value;
            }
        }
        public override bool ShowDeleteButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowDeleteButton;
            }
            set
            {
                base.ShowDeleteButton = value;
            }
        }
        public override bool ShowEditButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowEditButton;
            }
            set
            {
                base.ShowEditButton = value;
            }
        }
        public override bool ShowInsertButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowInsertButton;
            }
            set
            {
                base.ShowInsertButton = value;
            }
        }
        public override bool ShowSelectButton
        {
            get
            {
                return base.Control is GridView ? false : base.ShowSelectButton;
            }
            set
            {
                base.ShowSelectButton = value;
            }
        }

        public override string CancelText
        {
            get
            {
                return base.CancelText;
            }
        }
        public override string DeleteText
        {
            get
            {
                return base.DeleteText;
            }
        }
        public override string EditText
        {
            get
            {
                return base.EditText;
            }
        }
        public override string InsertText
        {
            get
            {
                return base.InsertText;
            }
        }
        public override string SelectText
        {
            get
            {
                return base.SelectText;
            }
        }
        public override string UpdateText
        {
            get
            {
                return base.UpdateText;
            }
        }

        public override ButtonType ButtonType
        {
            get
            {
                return base.Control is GridView ? ButtonType.Link : ButtonType.Button;
            }
        }

        protected override DataControlField CreateField()
        {
            return new ActionField();
        }

        protected override void CopyProperties(DataControlField newField)
        {
            ((ActionField)newField).ShowViewButton = this.ShowViewButton;
            ((ActionField)newField).ShowModifyButton = this.ShowModifyButton;
            ((ActionField)newField).ShowDisableButton = this.ShowDisableButton;

            ((ActionField)newField).ShowInsertMoreButton = this.ShowInsertMoreButton;
            ((ActionField)newField).ShowCloseButton = this.ShowCloseButton;
            base.CopyProperties(newField);
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);

            bool showViewButton = this.ShowViewButton;
            bool showModifyButton = this.ShowModifyButton;
            bool showDisableButton = this.ShowDisableButton;
            bool showInsertMoreButton = this.ShowInsertMoreButton;
            bool showCloseButton = this.ShowCloseButton;

            if (cellType == DataControlCellType.DataCell)
            {
                // GridView
                if (this.ButtonType == ButtonType.Link)
                {
                    if (showViewButton)
                    {
                        this.AddLinkToCell(cell, rowIndex, "View", "entity-view");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                    if (showModifyButton)
                    {
                        this.AddLinkToCell(cell, rowIndex, "Modify", "entity-modify");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                    if (showDisableButton)
                    {
                        this.AddLinkToCell(cell, rowIndex, "Disable", "entity-disable");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                }
                // DetailsView
                else
                {
                    DecorateButtons(cell);
                    cell.Controls.Add(new LiteralControl("&nbsp;"));

                    if (GetDetailsViewMode() != DetailsViewMode.ReadOnly && showInsertMoreButton)
                    {
                        this.AddButtonToCell(cell, rowIndex, "InsertMore", "entity-insert-more");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                    if (GetDetailsViewMode() == DetailsViewMode.ReadOnly && showCloseButton)
                    {
                        this.AddButtonToCell(cell, rowIndex, "Close", "entity-close");
                        cell.Controls.Add(new LiteralControl("&nbsp;"));
                    }
                }
            }
        }

        private DetailsViewMode GetDetailsViewMode()
        {
            return (this.Control as DetailsView).CurrentMode;
        }

        private void DecorateButtons(DataControlFieldCell cell)
        {
            foreach (var ctl in cell.Controls)
            {
                if (ctl is Button)
                {
                    Button button = ctl as Button;

                    if (button.Text == CancelText)
                        button.CssClass = "entity-cancel";

                    else if (button.Text == DeleteText)
                        button.CssClass = "entity-delete";

                    else if (button.Text == InsertText)
                        button.CssClass = "entity-save";

                    else if (button.Text == EditText)
                        button.CssClass = "entity-modify";

                    else if (button.Text == SelectText)
                        button.CssClass = "entity-select";

                    else if (button.Text == UpdateText)
                        button.CssClass = "entity-save";
                }
            }
        }

        private void AddLinkToCell(DataControlFieldCell cell, int rowIndex, string command, string style)
        {
            LinkButton anchor = new LinkButton()
            {
                CssClass = style,
                CommandName = command,
                CommandArgument = rowIndex.ToString(),
            };
            cell.Controls.Add(anchor);
        }

        private void AddButtonToCell(DataControlFieldCell cell, int rowIndex, string command, string style)
        {
            Button button = new Button()
            {
                CssClass = style,
                CommandName = command,
                CommandArgument = rowIndex.ToString(),
            };
            cell.Controls.Add(button);
        }
    }
}
